﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WaterRevenueMS.Core.Repository.Auth;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.Authen.Module;
using WaterRevenueMS.Componnent.Entities.Authen;
using WaterRevenueMS.Core.Repository.Auth.Impl;
using System.ComponentModel.Composition;

namespace WaterRevenueMS.Core.Service.Authen.Impl
{
    [Export(typeof(IModuleService))]
    public class ModuleService:CoreServiceBase,IModuleService
    {
        #region 属性
        [Import]
        public IModuleRepository ModuleRepository { get; set; }

        //public ModuleService()
        //{
        //    ModuleRepository = new ModuleRepository();
        //}
        public IQueryable<Module> Modules
        {
            get { return ModuleRepository.Entities; }
        }

        #endregion

        #region 公共方法

        public OperationResult Insert(ModuleModel model)
        {
            var entity = new Module
            {
                Name = model.Name,
                Code = model.Code,
                ParentId = model.ParentId != 0 ? model.ParentId : null,
                LinkUrl = model.LinkUrl,
                Area = model.Area,
                Controller = model.Controller,
                Action = model.Action,
                OrderSort = model.OrderSort,
                Icon = model.Icon != null ? model.Icon : "",
                Enabled = model.Enabled
            };
            ModuleRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public OperationResult Update(ModuleModel model)
        {
            var entity = new Module
            {
                Id = model.Id,
                Name = model.Name,
                Code = model.Code,
                ParentId = model.ParentId != 0 ? model.ParentId : null,
                LinkUrl = model.LinkUrl,
                Area = model.Area,
                Controller = model.Controller,
                Action = model.Action,
                OrderSort = model.OrderSort,
                Icon = model.Icon != null ? model.Icon : "",
                Enabled = model.Enabled,
                IsMenu=model.IsMenu
            };
            ModuleRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public OperationResult Delete(int Id)
        {
            var model = Modules.FirstOrDefault(t => t.Id == Id);
            model.IsDeleted = true;

            ModuleRepository.Update(model);
            return new OperationResult(OperationResultType.Success, "删除成功");
        }

        #endregion
    }
}
